package com.example.administrator.college02;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.example.administrator.college02.model.Banner;
import com.example.administrator.college02.pager.CycleViewPager;
import com.example.administrator.college02.utils.AppLog;

import java.util.List;

import butterknife.Unbinder;
import okhttp3.OkHttpClient;


/**
 * Author ： LongWeiHu on 2016/5/17.
 */
public class BaseFragment extends Fragment {
    protected InputMethodManager inputMethodManager;
    protected BaseActivity mBaseActivity;
    protected Bundle mBundle;
    protected Context mContext;
    public Unbinder unbinder = null;
    public CycleViewPager mCycleViewPager = null;
    public List<Banner> banners = null;
    public OkHttpClient client;
    public FragmentManager fm ;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = getArguments();
        if (mBundle == null) {
            mBundle = new Bundle();
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mContext = getActivity().getApplicationContext();
        client = new OkHttpClient();
        inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        mBaseActivity = (BaseActivity) getActivity();
        fm = mBaseActivity.getSupportFragmentManager();
        initView(view);
        setListener();
    }

    protected void setListener() {
    }

    protected void initView(View view) {

    }


    /**
     * 返回键处理
     *
     * @return
     */
    public boolean onBackPressed() {
        return false;
    }


    /**
     * 关闭Framgment
     */
    public void popSelf() {
        popBackStack();
        hideSoftKeyboard();
    }

    public void popSelf(String tag) {
        try {
            if (isDetached() || isRemoving() || getFragmentManager() == null) {
                return;
            }
            if (isResumed()) {
                getFragmentManager().popBackStackImmediate(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }
            hideSoftKeyboard();
        } catch (Exception e) {
            AppLog.instance().e(e.getMessage());
        }

    }

    private void popBackStack() {
        try {
            if (isDetached() || isRemoving() || getFragmentManager() == null) {
                return;
            }
            if (isResumed()) {
                getFragmentManager().popBackStackImmediate();
            }
        } catch (Exception e) {
            AppLog.instance().e(e.getMessage());
        }
    }
    protected void hideSoftKeyboard() {
        if (getActivity().getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getActivity().getCurrentFocus() != null)
                if (inputMethodManager != null) {
                    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
        }
    }

}